let storage = bui.storage();
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            language = storage.get("language", 0)
            translate = global.translate
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#userManagerLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });
            let uiLoadingBtn = bui.loading({
                appendTo:"#userManagerLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"提交中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            let token = storage.get("token",0)
            let user = storage.get("user",0)
            let id;

            // 自定义居中弹出框
            let qxDialog = bui.dialog({
                id: "#qxDialog",
                height: 300,
                // mask: false,
                callback: function(e) {}
            });

            router.$("#userManagerFh").click(function () {
                storage.set("tzPage", '0')
                router.replace({url: "pages/mainIndex/mainIndex.html"});
            })

            if (token) {
                // uiLoading.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        storage.set("dlTz", '13')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                })


                let uiList = bui.list({
                    id: "#userManagerScrollList",
                    url: global.api.getUserList,
                    pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
                    data: {},
                    headers: {Token: token},
                    //如果分页的字段名不一样,通过field重新定义
                    field: {
                        page: "page",
                        size: "limit",
                        data: "data"
                    },
                    callback: function(e) {
                        // e.target 为你当前点击的元素
                        // e.currentTarget 为你当前点击的handle 整行
                        let cla = router.$(e.target).attr("class")
                        id = router.$(e.target).attr("name")
                        let qxStr = router.$(e.target).attr("qxStr")

                        if (cla.indexOf("edit") > -1) {
                            //编辑
                            // storage.set('articleEditId', id)
                            // router.replace({ url: "pages/articleEdit/articleEdit.html"});
                        } else if (cla.indexOf("delete") > -1) {
                            //删除
                            bui.confirm("确认要删除此信息么？", function(e) {
                                //this 是指点击的按钮
                                let text = router.$(e.target).text();

                                if (text == "确定") {
                                    uiLoadingBtn.show()
                                    bui.ajax({
                                        url: global.api.deleteUser,
                                        data:{userId: id},
                                        method: "POST"
                                    }).then(function(res){
                                        if (res.code == '200') {
                                            uiList.refresh();
                                            bui.confirm({
                                                "title": "",
                                                "height": 460,
                                                "content": '<div class="bui-box-center"><div><i class="icon-successfill success"></i><h3>删除成功</h3></div></div>',
                                                "buttons": [{ name: "我知道了", className: "primary-reverse" }]
                                            });
                                        }
                                        uiLoadingBtn.stop()
                                    })
                                }
                                this.close()
                            });
                        } else if (cla.indexOf("qx") > -1) {
                            router.$("#box1").prop('checked',false);
                            router.$("#box5").prop('checked',false);
                            router.$("#box3").prop('checked',false);
                            router.$("#box4").prop('checked',false);
                            if (qxStr && qxStr != 'null') {
                                let arr = qxStr.split(',')
                                $.each(arr, function (index, value) {
                                    switch (value) {
                                        case "0":
                                            router.$("#box1").prop('checked', 'true')
                                            break
                                        case "2":
                                            router.$("#box3").prop('checked', 'true')
                                            break
                                        case "3":
                                            router.$("#box4").prop('checked', 'true')
                                            break
                                        case "4":
                                            router.$("#box5").prop('checked', 'true')
                                            break
                                    }
                                })
                            }
                            //授权
                            qxDialog.open()
                        }
                    },
                    template: function(data) {
                        let html = "";
                        data.forEach(function(el, index) {
                            let statusStr = ''
                            // html += '<li class="bui-btn">\n' +
                            //     '<div class="bui-box">'+
                            //     '                <div class="bui-thumbnail" style="width: 1.5rem;height: 1.5rem"><img src="'+el.avatar+'" alt=""></div>\n' +
                            //     '                <div class="span1">\n' +
                            //     '                    <h3 class="item-title">'+el.nickName+'</h3>\n'
                            html += '<li class="bui-btn">\n' +
                                '<div class="bui-box">'+
                                '                <div class="span1">\n' +
                                '                    <h3 class="item-title">'+el.nickName+'</h3>\n'

                            let btnStr = ''
                            if(el.status == '0') {
                                statusStr = '停用'
                                btnStr =  '                            <div class="bui-box" style="margin-top: 10px">\n' +
                                    // '                                <div class="span1" style="display: none">\n' +
                                    // '                                    <div class="czbtn edit" style="background: #00B066;" name="'+el.userId+'">\n' +
                                    // '                                        编辑\n' +
                                    // '                                    </div>\n' +
                                    // '                                </div>\n' +
                                    '                                <div class="span1">\n' +
                                    '                                    <div class="czbtn delete" style="background: red;" name="'+el.userId+'">\n' +
                                    '                                        删除\n' +
                                    '                                    </div>\n' +
                                    '                                </div>\n'+
                                    '                                <div class="span1">\n' +
                                    '                                </div>\n'
                                    '                        </div>\n'
                            } else if (el.status == '1') {
                                statusStr = '启用'
                                btnStr =  '                            <div class="bui-box" style="margin-top: 10px">\n' +
                                    // '                                <div class="span1" style="display: none">\n' +
                                    // '                                    <div class="czbtn edit" style="background: #00B066;" name="'+el.userId+'">\n' +
                                    // '                                        编辑\n' +
                                    // '                                    </div>\n' +
                                    // '                                </div>\n' +
                                    '                                <div class="span1">\n' +
                                    '                                    <div class="czbtn delete" style="background: red;" name="'+el.userId+'">\n' +
                                    '                                        删除\n' +
                                    '                                    </div>\n' +
                                    '                                </div>\n'+
                                    '                                <div class="span1">\n' +
                                        '                                    <div class="czbtn qx" style="background: #ff841d;" name="'+el.userId+'" qxStr="'+el.qxStr+'">\n' +
                                        '                                        授权\n' +
                                        '                                    </div>\n' +
                                        '                                </div>\n'
                                btnStr += '                        </div>\n'
                            }

                            html +=  '                    <p class="item-text" style="color: red">'+statusStr+'</p>\n' +
                                '                </div></div>\n' +
                                btnStr+'            </li>'
                        });
                        uiLoading.stop()

                        setTimeout(function () {
                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        },1000)
                        return html;
                    }
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '13')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }

            router.$("#qd").click(function () {
                uiLoadingBtn.show()
                let qxStr = ''
                if (router.$("#box1").prop("checked")) {
                    qxStr += router.$("#box1").val() +","
                }
                if (router.$("#box3").prop("checked")) {
                    qxStr += router.$("#box3").val() +","
                }
                if (router.$("#box4").prop("checked")) {
                    qxStr += router.$("#box4").val() +","
                }
                if (router.$("#box5").prop("checked")) {
                    qxStr += router.$("#box5").val() +","
                }

                if (qxStr) {
                    qxStr = qxStr.substring(0, qxStr.length - 1)
                }

                bui.ajax({
                    url: global.api.sqQx,
                    data: {userId: id, qxStr: qxStr, city: user.city},
                    method: "POST"
                }).then(function(res){
                    if (res.code == '200') {
                        bui.alert("授权成功")
                    } else {
                        if (res.msg.indexOf('该文章审核权限已存在') > -1) {
                            bui.alert(res.msg)
                        } else {
                            bui.alert("授权失败")
                        }
                    }
                    uiLoadingBtn.stop()
                })
            })

            try {
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
